package mission_tour.solution;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.FrameLayout.LayoutParams;

public class tour_map extends Activity implements ServerIp{
    /** Called when the activity is first created. */
	static int mapleft;
	static int maptop;
	static int mapright;
	static int mapbottom;
	
	int mapX = 0;
	int mapY = 0;
	
	AlertDialog.Builder nologin;
	AbsoluteLayout .LayoutParams abparam;
	ImageView myposition;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tour_map);
        
        items.event_list.removeAll(items.event_list);
        
        Toast.makeText(tour_map.this,"장소이름을 터치 하시면 해당 위치의 정보를 보실 수 있습니다",Toast.LENGTH_SHORT).show();
        
        final LayoutParams flpr = new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT);
        addContentView(new createnemo(this),flpr);	//터치시 그려지는 이미지뷰
        
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);	//	화면 안꺼짐
        
        final AbsoluteLayout map_al = (AbsoluteLayout) findViewById(R.id.map_al);
        ImageView map = (ImageView) findViewById(R.id.map_img_test);
        
        abparam = new AbsoluteLayout .LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, mapX, mapY);	//내 위치 표시되는 좌표 파람 (가로크기 , 세로크기 , X좌표 , Y좌표)
        myposition = new ImageView(this);
        
//        final Handler tsHandler = new Handler();
//        tsHandler.postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				tsHandler.postDelayed(this, 3000);
//				
//				for(int i=0; i<items.map_myposition_x.length;i++){
//					if(((items.my_positionX <= (items.map_position_list.get(i).getP_x()+0.0001)) && (items.my_positionX >= (items.map_position_list.get(i).getP_x() - 0.0001)))&& ((items.my_positionY <= (items.map_position_list.get(i).getP_y()+0.0001)) && (items.my_positionX >= (items.map_position_list.get(i).getP_y() - 0.0001)))){
//						mapX = items.map_myposition_x[i];
//						mapY = items.map_myposition_y[i];
//					}
//				}
//				
//				abparam.x = mapX-20;
//				abparam.y = mapY-20;
//				
//				myposition.setLayoutParams(abparam);
//				
//			}
//    	}, 3000); 
//    	
//        myposition.setBackgroundResource(R.drawable.myposition);
//        map_al.addView(myposition);
        
        map.setOnTouchListener(new View.OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
					if (((event.getX() >= 283) && (event.getX() <= 386)) && ((event.getY() >= 563) && (event.getY() <= 607))) {
						items.map_position = items.SHINPOONGLOO;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					
					if (((event.getX() >= 294) && (event.getX() <= 384)) && ((event.getY() >= 370) && (event.getY() <= 411))) {
						items.map_position = items.LEFTICMOON;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					
					if (((event.getX() >= 284) && (event.getX() <= 377)) && ((event.getY() >= 277) && (event.getY() <= 321))) {
						items.map_position = items.CENTERMOON;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 301) && (event.getX() <= 395)) && ((event.getY() >= 140) && (event.getY() <= 187))) {
						items.map_position = items.BONGSOODANG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 189) && (event.getX() <= 283)) && ((event.getY() >= 594) && (event.getY() <= 635))) {
						items.map_position = items.NAMGUNYOUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 150) && (event.getX() <= 244)) && ((event.getY() >= 509) && (event.getY() <= 551))) {
						items.map_position = items.SURYCHUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 150) && (event.getX() <= 244)) && ((event.getY() >= 440) && (event.getY() <= 483))) {
						items.map_position = items.BIGANGCHUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 156) && (event.getX() <= 274)) && ((event.getY() >= 346) && (event.getY() <= 386))) {
						items.map_position = items.JUNGLISO;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 146) && (event.getX() <= 236)) && ((event.getY() >= 257) && (event.getY() <= 302))) {
						items.map_position = items.YOUYUTACK;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 236) && (event.getX() <= 331)) && ((event.getY() >= 195) && (event.getY() <= 241))) {
						items.map_position = items.GYUNHRYONGGWAN;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 237) && (event.getX() <= 336)) && ((event.getY() >= 90) && (event.getY() <= 140))) {
						items.map_position = items.JANGRAKDANG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 141) && (event.getX() <= 228)) && ((event.getY() >= 150) && (event.getY() <= 190))) {
						items.map_position = items.BOCNAEDANG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 424) && (event.getX() <= 525)) && ((event.getY() >= 601) && (event.getY() <= 646))) {
						items.map_position = items.BOOKGUNYOUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 424) && (event.getX() <= 525)) && ((event.getY() >= 441) && (event.getY() <= 489))) {
						items.map_position = items.ZIPSACHUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 436) && (event.getX() <= 532)) && ((event.getY() >= 165) && (event.getY() <= 207))) {
						items.map_position = items.NACNAMHUN;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 434) && (event.getX() <= 525)) && ((event.getY() >= 37) && (event.getY() <= 79))) {
						items.map_position = items.DKJOONGJUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}

					if (((event.getX() >= 419) && (event.getX() <= 515)) && ((event.getY() >= 112) && (event.getY() <= 157))) {
						items.map_position = items.NORAEDANG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 666) && (event.getX() <= 758)) && ((event.getY() >= 152) && (event.getY() <= 196))) {
						items.map_position = items.WNHANGAK;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 831) && (event.getX() <= 927)) && ((event.getY() >= 254) && (event.getY() <= 298))) {
						items.map_position = items.POONGHWADANG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 772) && (event.getX() <= 865)) && ((event.getY() >= 152) && (event.getY() <= 196))) {
						items.map_position = items.IANCHUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 510) && (event.getX() <= 607)) && ((event.getY() >= 231) && (event.getY() <= 273))) {
						items.map_position = items.JUNSACHUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 674) && (event.getX() <= 770)) && ((event.getY() >= 298) && (event.getY() <= 436))) {
						items.map_position = items.NAESAMMOON;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
					if (((event.getX() >= 587) && (event.getX() <= 657)) && ((event.getY() >= 129) && (event.getY() <= 173))) {
						items.map_position = items.JAEJUNG;
						
						Intent intent = new Intent(tour_map.this, tour_tourinfo.class);
						startActivityForResult(intent, 0);
						finish();
					}
				}else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
				}
				return false;
			}
		});
    }
    
    private static class createnemo extends FrameLayout{
    	public createnemo(Context context){
    		super(context);
    	}
    	protected void onDraw(Canvas canvas){
    		Paint nemopaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    		nemopaint.setColor(Color.parseColor("#606495ED"));
    		canvas.drawRoundRect(new RectF(mapleft, maptop, mapright, mapbottom),10, 10,nemopaint);
    	}
    }
        
    public boolean onKeyDown( int KeyCode, KeyEvent event ){
		if( event.getAction() == KeyEvent.ACTION_DOWN ){
			if( KeyCode == KeyEvent.KEYCODE_BACK ){
				Intent intent = new Intent(tour_map.this, tour_main.class);
				startActivityForResult(intent, 0);
				finish();
				
				return false;
			}
		}
		return super.onKeyDown(KeyCode, event);
	}
    public boolean onCreateOptionsMenu(Menu menu){
    	getMenuInflater().inflate(R.menu.quick, menu);
		return true;   	
    }

    public boolean onOptionsItemSelected(MenuItem item){
    	if(item.getItemId() == R.id.map_go){
    		startActivity(new Intent(this,tour_map.class));
    		finish();
    		return true;
    	}else if(item.getItemId() == R.id.event_go){
    		startActivity(new Intent(this,tour_event_info.class));
    		finish();
    		return true;
    	}else if(item.getItemId() == R.id.myinfo_go){
    		startActivity(new Intent(this,tour_myinfo.class));
    		finish();
    		return true;
    	}else if(item.getItemId() == R.id.coupon_go){
    		startActivity(new Intent(this,tour_coupon.class));
    		finish();
    		return true;
    	}else if(item.getItemId() == R.id.help_go){
    		startActivity(new Intent(this,tour_help.class));
    		finish();
    		return true;
    	}else if(item.getItemId() == R.id.oxquiz_go){
    		items.event_list.removeAll(items.event_list);

    		new Thread(new Runnable() {    
				@Override
				public void run() {
					runOnUiThread(new Runnable(){
						 @Override
						 public void run() {
							 AlertDialog.Builder q_level = new AlertDialog.Builder(tour_map.this)
				    			.setTitle(items.appname)
				    			.setMessage("퀴즈 난이도를 선택해 주세요")
				    			.setIcon(R.drawable.icon)
				    			.setPositiveButton("초급", new DialogInterface.OnClickListener(){
				    				@Override
				    				public void onClick(DialogInterface dialog, int which) {
				    					// TODO Auto-generated method stub
				    					final ProgressDialog nowloding = ProgressDialog.show(tour_map.this,"","OX 퀴즈를 불러오고 있습니다....",true);
				    			        
				    			        new Thread(new Runnable() {    
				    						@Override
				    						public void run() {				 
				    							try{
				    				    			
				    					    		URL url = null;
				    						        String resultString =null;
				    						  
				    					        	url = new URL(tour_oxquiz_call+"?app_type="+items.apptype+"&q_level="+items.q_level_easy);
				    							
				    					        	HttpURLConnection http = (HttpURLConnection)url.openConnection();  
				    					        	
				    					        	BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));  
				    					        	StringBuffer buffer = new StringBuffer();
				    								 
				    					        	int z;
				    					        	
				    								while((z=in.read()) != -1){
				    									buffer.append((char)z);
				    								}
				    								
				    								resultString= buffer.toString().trim();
				    								
				    					            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				    					            factory.setNamespaceAware(true);
				    					            
				    					            boolean tagok = false;
				    					            
				    					            String[] quiz_call = null;
				    					            
				    					            XmlPullParser xpp = factory.newPullParser();
				    					            xpp.setInput(new StringReader(resultString));
				    					            
				    					            int eventType = xpp.getEventType();
				    					            
				    					            while (eventType != XmlPullParser.END_DOCUMENT) {
				    					                
				    					            	if(eventType == XmlPullParser.START_DOCUMENT) {
				    					                    System.out.println("Start document");
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.END_DOCUMENT) {
				    					                    System.out.println("End document");
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.START_TAG) {
				    					            		
				    					            		if(xpp.getName().equals("name")){
				    					            			tagok = true;
				    					            		}
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.END_TAG) {
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.TEXT) {
				    					            		if(tagok){
				    					            			quiz_call = xpp.getText().split(",");
				    					                	}
				    					            		quizs qs = new quizs("",quiz_call[0],"","","","","",Integer.parseInt(quiz_call[1]),0,quiz_call[2],Integer.parseInt(quiz_call[3]));
				    					            
				    					            		items.event_list.add(qs);
				    					                }
				    					                eventType = xpp.next();
				    					            }
				    					            
				    					            nowloding.cancel();
				    					            
				    					            Intent intent = new Intent(tour_map.this, tour_oxquiz.class);
				    								startActivityForResult(intent, 0);
				    								finish();
				    					    		
				    							} catch (MalformedURLException e) {
				    								// TODO Auto-generated catch block
				    								e.printStackTrace();
				    							} catch (IOException e) {
				    								// TODO Auto-generated catch block								
				    								runOnUiThread(new Runnable(){
				    						            @Override
				    						             public void run() {
				    						            	AlertDialog.Builder nologin = new AlertDialog.Builder(tour_map.this);
				    						    			nologin.setTitle(items.appname);
				    						    			nologin.setMessage("서버에 접속이 되지 않습니다\n네트워크를 확인 후 다시 시도해 주세요");
				    						    			nologin.setIcon(R.drawable.icon);
				    						    			nologin.setPositiveButton("확 인", new DialogInterface.OnClickListener(){
				    						    				@Override
				    						    				public void onClick(DialogInterface dialog, int which) {
				    						    					// TODO Auto-generated method stub
				    						    					moveTaskToBack(true);
				    						    					android.os.Process.killProcess(android.os.Process.myPid());
				    						    				}
				    						    			});
				    						    			nologin.show();
				    						             }
				    						        });
				    							}catch(Exception e){
				    								System.out.println(e.toString());
				    							}
				    						}
				    				    }).start();
				    				}
				    			})
				    			.setNeutralButton("고급", new DialogInterface.OnClickListener(){
				    				@Override
				    				public void onClick(DialogInterface dialog, int which) {
				    					// TODO Auto-generated method stub
				    					final ProgressDialog nowloding = ProgressDialog.show(tour_map.this,"","OX 퀴즈를 불러오고 있습니다....",true);
				    			        
				    			        new Thread(new Runnable() {    
				    						@Override
				    						public void run() {				 
				    							try{
				    				    			
				    					    		URL url = null;
				    						        String resultString =null;
				    						  
				    					        	url = new URL(tour_oxquiz_call+"?app_type="+items.apptype+"&q_level="+items.q_level_hard);
				    							
				    					        	HttpURLConnection http = (HttpURLConnection)url.openConnection();  
				    					        	
				    					        	BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));  
				    					        	StringBuffer buffer = new StringBuffer();
				    								 
				    					        	int z;
				    					        	
				    								while((z=in.read()) != -1){
				    									buffer.append((char)z);
				    								}
				    								
				    								resultString= buffer.toString().trim();
				    								
				    					            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				    					            factory.setNamespaceAware(true);
				    					            
				    					            boolean tagok = false;
				    					            
				    					            String[] quiz_call = null;
				    					            
				    					            XmlPullParser xpp = factory.newPullParser();
				    					            xpp.setInput(new StringReader(resultString));
				    					            
				    					            int eventType = xpp.getEventType();
				    					            
				    					            while (eventType != XmlPullParser.END_DOCUMENT) {
				    					                
				    					            	if(eventType == XmlPullParser.START_DOCUMENT) {
				    					                    System.out.println("Start document");
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.END_DOCUMENT) {
				    					                    System.out.println("End document");
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.START_TAG) {
				    					            		
				    					            		if(xpp.getName().equals("name")){
				    					            			tagok = true;
				    					            		}
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.END_TAG) {
				    					                }
				    					            	
				    					            	if (eventType == XmlPullParser.TEXT) {
				    					            		if(tagok){
				    					            			quiz_call = xpp.getText().split(",");
				    					                	}
				    					            		quizs qs = new quizs("",quiz_call[0],"","","","","",Integer.parseInt(quiz_call[1]),0,quiz_call[2],Integer.parseInt(quiz_call[3]));
				    					            
				    					            		items.event_list.add(qs);
				    					                }
				    					                eventType = xpp.next();
				    					            }
				    					            
				    					            nowloding.cancel();
				    					            
				    					            Intent intent = new Intent(tour_map.this, tour_oxquiz.class);
				    								startActivityForResult(intent, 0);
				    								finish();
				    					    		
				    							} catch (MalformedURLException e) {
				    								// TODO Auto-generated catch block
				    								e.printStackTrace();
				    							} catch (IOException e) {
				    								// TODO Auto-generated catch block								
				    								runOnUiThread(new Runnable(){
				    						            @Override
				    						             public void run() {
				    						            	AlertDialog.Builder nologin = new AlertDialog.Builder(tour_map.this);
				    						    			nologin.setTitle(items.appname);
				    						    			nologin.setMessage("서버에 접속이 되지 않습니다\n네트워크를 확인 후 다시 시도해 주세요");
				    						    			nologin.setIcon(R.drawable.icon);
				    						    			nologin.setPositiveButton("확 인", new DialogInterface.OnClickListener(){
				    						    				@Override
				    						    				public void onClick(DialogInterface dialog, int which) {
				    						    					// TODO Auto-generated method stub
				    						    					moveTaskToBack(true);
				    						    					android.os.Process.killProcess(android.os.Process.myPid());
				    						    				}
				    						    			});
				    						    			nologin.show();
				    						             }
				    						        });
				    							}catch(Exception e){
				    								System.out.println(e.toString());
				    							}
				    						}
				    				    }).start();
				    				}
				    			});
								q_level.show();
						 }
					 });
				}
		    }).start();
    	}
		return false;
    }
}
